package com.experiment.aicommunityback.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.experiment.aicommunityback.entity.Goods;
import com.experiment.aicommunityback.entity.GoodsType;
import com.experiment.aicommunityback.entity.Result;
import com.experiment.aicommunityback.entity.ResultCodeEnum;
import com.experiment.aicommunityback.entity.dto.GoodsDTO;
import com.experiment.aicommunityback.mapper.GoodsMapper;
import com.experiment.aicommunityback.mapper.GoodsTypeDTOMapper;
import com.experiment.aicommunityback.mapper.GoodsTypeFirstChildDTOMapper;
import com.experiment.aicommunityback.mapper.GoodsTypeMapper;
import com.experiment.aicommunityback.service.GoodsService;

import org.springframework.beans.factory.annotation.Autowired;

import com.experiment.aicommunityback.entity.dto.GoodsTypeFirstChildDTO;
import com.experiment.aicommunityback.entity.dto.GoodsTypeDTO;
import com.experiment.aicommunityback.entity.dto.GoodsTypeSecondChildDTO;

import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class GoodsServiceimpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsTypeDTOMapper goodsTypeDTOMapper;
    @Autowired
    private GoodsTypeMapper goodsTypeMapper;
    @Autowired
    private GoodsTypeFirstChildDTOMapper goodsTypeFirstChildDTOMapper;

    @Override
    public Result getAll() {
        List<Goods> goods = goodsMapper.selectList(null);
        Result result = Result.setResult(ResultCodeEnum.SUCCESS);
        Map<String, Object> goodsmap = new HashMap<>();
        goodsmap.put("Goods", goods);
        result.setData(goodsmap);
        return result;
    }

    public Result selectGoodsTypeList() {
        List<GoodsTypeDTO> goodsTypeList = new ArrayList<>();
//        返回的一个包含所有super_type为0的类型列表
        List<GoodsTypeFirstChildDTO> GoodsTypefirstList = goodsMapper.selectGoodsTypeFirstList();
        for (GoodsTypeFirstChildDTO dto : GoodsTypefirstList) {
//            这个goodsTypeListDTO就是
            GoodsTypeDTO goodsTypeDTO = new GoodsTypeDTO();
//            子类列表初始化 然后在循环里面加
            List<GoodsTypeSecondChildDTO> childrenList = new ArrayList<>();
            List<GoodsTypeSecondChildDTO> goodsTypeSecondChildList = goodsMapper.selectSecondList(dto.getId());
            //把查出来的子类列表+第一个循环遍历的GoodsTypeFirstChildDTO加进goodsTypeDTO
            for (GoodsTypeSecondChildDTO goodsTypeSecondChildDTO : goodsTypeSecondChildList
            ) {
                childrenList.add(goodsTypeSecondChildDTO);
            }
            goodsTypeDTO.setId(dto.getId());
            goodsTypeDTO.setTypeName(dto.getTypeName());
            goodsTypeDTO.setSuperType(dto.getSuperType());
            goodsTypeDTO.setChildren(childrenList);
//                再把goodsTypeDTO加进goodsTypeList
            goodsTypeList.add(goodsTypeDTO);
        }

        Result result = Result.setResult(ResultCodeEnum.SUCCESS);
        Map<String, Object> goodsTypeListMap = new HashMap<>();
        goodsTypeListMap.put("goodsTypeList", goodsTypeList);
        result.setData(goodsTypeListMap);
        return result;
    }


    @Override
    public Result delete(List<Integer> arrayData) {
        int i = goodsMapper.deleteBatchIds(arrayData);
        return this.getAll();
    }

    @Override
    public Result Conditional_Query(Map<String, Object> paramMap) {
        Result result = Result.setResult(ResultCodeEnum.SUCCESS);
        if (Objects.equals(paramMap.get("shelves"), "all") ||paramMap.get("shelves")=="") {
            paramMap.remove("shelves");
        }
        else {
            paramMap.put("shelves", Boolean.valueOf(paramMap.get("shelves").toString()));
        }
        if (Objects.equals(paramMap.get("audit"), "all") ||paramMap.get("audit")==""){
            paramMap.remove("audit");
        }
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();

        // 先将paramMap中的非name相关的条件添加到QueryWrapper中（假设paramMap中的键对应数据库表中的字段名）

        paramMap.forEach((key, value) -> {
            if (value!= "" &&!"name".equals(key)) {
                System.out.println(key);
                System.out.println(value);
                queryWrapper.eq(key, value);
            }
        });
        if (paramMap.containsKey("name")) {
            String name = (String) paramMap.get("name");
            queryWrapper.like("name", name);
        }
        List<Goods> goods1 = goodsMapper.selectList(queryWrapper);
        for (Goods goods : goods1) {
            System.out.println(goods);
        }
        Map<String, Object> goodsmap = new HashMap<>();
        goodsmap.put("Goods",goods1);
        result.setData(goodsmap);
        return result;
    }


    @Override
    public Result addGood(Goods goods) {
            goodsMapper.insertGood(goods);
            Result result = Result.setResult(ResultCodeEnum.SUCCESS);
            return result;
    }

    @Override
    public Result addGoodsType(GoodsTypeFirstChildDTO goodstype) {
        goodsMapper.insertGoodsType(goodstype);
        Result result = Result.setResult(ResultCodeEnum.SUCCESS);
        return result;
    }

    @Override
    public Result changeShelves(Integer id) {
        Goods good = goodsMapper.selectById(id);
        System.out.println(good);
        System.out.println(good.isShelves());
//        将Shelves值取反
        boolean goodShelves = good.isShelves();
        good.setShelves(!goodShelves);
        goodsMapper.updateById(good);
        Result result = Result.setResult(ResultCodeEnum.SUCCESS);
        return result;
    }

    @Override
    public Result changeNewProduct(Integer id) {
        Goods good = goodsMapper.selectById(id);
//        将NewProduct值取反
        boolean goodNewProduct = good.isNewProduct();
        good.setNewProduct(!goodNewProduct);
        goodsMapper.updateById(good);
        Result result = Result.setResult(ResultCodeEnum.SUCCESS);
        return result;
    }

    @Override
    public Result changeRecommend(Integer id) {
        Goods good = goodsMapper.selectById(id);
//        将Recommend值取反
        boolean goodNewProduct = good.isRecommend();
        good.setRecommend(!goodNewProduct);
        goodsMapper.updateById(good);
        Result result = Result.setResult(ResultCodeEnum.SUCCESS);
        return result;
    }

    @Override
    public Result getSecondTypeGoodsList() {
        List<GoodsTypeSecondChildDTO> secondTypeGoodsList = goodsTypeMapper.getSecondTypeGoodsList();

        Result result = Result.setResult(ResultCodeEnum.SUCCESS);
        Map<String, Object> secondGoodsTypeListMap = new HashMap<>();
        secondGoodsTypeListMap.put("secondTypeGoodsList", secondTypeGoodsList);
        result.setData(secondGoodsTypeListMap);
        return result;
    }

    @Override
    public Result updateGood(Goods good) {
        goodsMapper.updateById(good);
        List<Goods> goods = goodsMapper.selectList(null);
        Result result = Result.setResult(ResultCodeEnum.SUCCESS);
        Map<String, Object> goodsmap = new HashMap<>();
        goodsmap.put("Goods", goods);
        result.setData(goodsmap);
        return result;
    }

    @Override
    public Result getAllFirstGoodsType() {
        List<GoodsTypeFirstChildDTO> goodsTypes = goodsTypeMapper.getAllFirstGoodsType();
        Result result = Result.setResult(ResultCodeEnum.SUCCESS);
        Map<String, Object> getAllFirstGoodsTypeListMap = new HashMap<>();
        getAllFirstGoodsTypeListMap.put("addBelongingTypeValue", goodsTypes);
        result.setData(getAllFirstGoodsTypeListMap);
        return result;
    }

    @Override
    public Result updateGoodsType(GoodsTypeFirstChildDTO goodstype) {
        goodsTypeFirstChildDTOMapper.updateById(goodstype);
        Result result = Result.setResult(ResultCodeEnum.SUCCESS);
        Map<String, Object> goodsTypeUpdate = new HashMap<>();
        goodsTypeUpdate.put("goodsTypeUpdate", null);
        result.setData(goodsTypeUpdate);
        return result;
    }
}
